<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\User;

class UserController extends CommonController {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $list = User::select(['id', 'username', 'logintime', 'updatetime', 'loginip', 'address'])->orderBy('id')->get();
        return view('admin.user.index', compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request){
        return view('admin.user.create', $this->getEdit($request->id));
    }

    public function insert (Request $request){
        if (!$request->isMethod('POST')) {
            return null;
        }

        $data = $request->except(['_token']);
        $data['inputtime'] = time();
        $data['username'] = 'Admin';
        $data['isreply'] = '1';
        $this->getInsert($data);
        return redirect('/admin/user');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        return $this->insert($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        return view('admin.user.index', $this->lists());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $data = $this->getEdit($id);
        return view('admin.user.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request) {
        if (!$request->isMethod('POST')) {
            return null;
        }

        $data = $request->except(['_token', '_method']);
        $user = $this->getUser();
        $userId = $user->id;
        $newpassword = $data['newpwd'];
        $msg = '';
        $code = 1;

        if ($newpassword != $data['renewpwd']) {
            $msg = '两次输入的密码不一致';
            return response()->json(compact('code', 'msg'));
        }


        /*
        $model = DB::table('user');
        $res = $model->where('id', $userId)->select('password')->first();
        dd($res->password);

        if(!Hash::check($data['oldpwd'], $res->password)){
            $msg = '旧密码不符或者用户名错误';
        } else {
            $update = [
                'password' => Hash::make($newpassword),
            ];
            $result = $model->where('id', $userId)->update($update);
            $msg = '密码修改成功';
            $code = 0;
        }
        */


        if(!Hash::check($data['oldpwd'], $user->password)){
            $msg = '旧密码不符或者用户名错误';
        } else {
            $user->password = Hash::make($newpassword);
            $user->logintime = time();
            $user->save();
            $code = 0;
            $msg = '密码修改成功';
            Auth::logout();
        }

        return response()->json(compact('code', 'msg'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $this->remove([$id]);
        return redirect('/admin/user');
    }
}
